package com.example.jl.bethinner.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.example.jl.bethinner.R;
import com.squareup.timessquare.CalendarPickerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;


public class TimesSquareActivity extends ActionBarActivity {


    private CalendarPickerView calendar;
    private SimpleDateFormat sdf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_times_square);
        Calendar nextYear = Calendar.getInstance();
        nextYear.add(Calendar.MONTH, 1);
        sdf = new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
        Date date1 = new Date(Long.valueOf("1468339200000"));
        Date date2 = new Date(Long.valueOf("1469030400000"));
        Date date3 = new Date(Long.valueOf("1468857600000"));

        Collection<Date> dates = new ArrayList<>();
        dates.add(date1);
        dates.add(date2);
        dates.add(date3);
        try {
            Date minDate = sdf.parse("2016-7-01");
            Date maxDate = sdf.parse("2016-8-0");
            calendar.init(minDate, maxDate)
                    .inMode(CalendarPickerView.SelectionMode.MULTIPLE)
                    .withSelectedDates(dates);

        } catch (ParseException e) {
            e.printStackTrace();
        }


//        calendar.selectDate(today);
//
//        List<String> dates = new ArrayList<>();
//        dates.add("2015-6-10");
//        dates.add("2015-6-11");
//        dates.add("2015-6-15");
//        stringToDate(dates);


        calendar.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
            @Override
            public void onDateSelected(Date date) {
                Log.d("data.time", date.getTime() + "");
            }

            @Override
            public void onDateUnselected(Date date) {

            }
        });
    }

    public void stringToDate(List<String> dates) {
        for (int i = 0; i < dates.size(); i++)
            try {
                Date date = sdf.parse(dates.get(i));
                calendar.selectDate(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_times_square, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
